package com.emoriarty.android.outilsresources.anim.button;

import android.app.Activity;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.Transformation;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;

import com.emoriarty.android.outilsresources.R;

public class ButtonDeployableTopActivity extends Activity {
	ImageButton buttonForeground;
	LinearLayout buttonBackground;
	
	SlidingDrawer buttonCameraBackground;
	ImageButton buttonCameraForeground;
	
	LinearLayout buttonAutomationBackground;
	ImageButton buttonAutomationForeground;
	LinearLayout buttonAutomationBackgroundOpen;
	ImageButton buttonAutomationForegroundOpen;
	
	Animation slideUpBack;
	Animation slideDownBack;
    Animation slideUpFore;
    Animation slideDownFore;
    
    Animation slideAutomationUpBack;
	Animation slideAutomationDownBack;
    Animation slideAutomationUpFore;
    Animation slideAutomationDownFore;
    
	float translationPercentage = 0.20f;
	boolean isPressed = false;
	boolean isAutomationPressed = false;
	int originalPosition;
	int originalPositionForeground;
	boolean isFirstTimeOpenBackground = true;
	boolean isFirstTimeOpenForeground = true;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.button_deployable);
        
        //First Test
        buttonBackground = (LinearLayout) findViewById(R.id.button_home_background);
        buttonForeground = (ImageButton) findViewById(R.id.button_home_foreground);
        buttonBackground.setOnClickListener(toggleButton);
        
        //Loading animations just one time
        slideUpBack = AnimationUtils.loadAnimation(ButtonDeployableTopActivity.this, R.anim.button_background_slide_up);
        slideDownBack = AnimationUtils.loadAnimation(ButtonDeployableTopActivity.this, R.anim.button_background_slide_down);
        slideUpFore = AnimationUtils.loadAnimation(ButtonDeployableTopActivity.this, R.anim.button_background_slide_up);
        slideDownFore = AnimationUtils.loadAnimation(ButtonDeployableTopActivity.this, R.anim.button_background_slide_down);
        
        //Setting the listeners
        slideUpBack.setAnimationListener(slideUpBackground);
        slideDownBack.setAnimationListener(slideDownBackground);
        slideUpFore.setAnimationListener(slideUpForeground);
        slideDownFore.setAnimationListener(slideDownForeground);
        
        //Second Test
        buttonCameraBackground = (SlidingDrawer) findViewById(R.id.button_camera_background);
        buttonCameraForeground = (ImageButton) findViewById(R.id.button_camera_foreground);
        
        buttonCameraBackground.setOnDrawerOpenListener(new OnDrawerOpenListener() {
			
			public void onDrawerOpened() {
				Animation slideDown = AnimationUtils.loadAnimation(ButtonDeployableTopActivity.this, R.anim.button_background_slide_down);
				buttonCameraForeground.startAnimation(slideDown);
			}
		});
        
        buttonCameraBackground.setOnDrawerCloseListener(new OnDrawerCloseListener() {
			
			public void onDrawerClosed() {
				Animation slideDown = AnimationUtils.loadAnimation(ButtonDeployableTopActivity.this, R.anim.button_background_slide_up);
				buttonCameraForeground.startAnimation(slideDown);
			}
		});
        
        //Third Test AUTOMATION
        buttonAutomationBackground = (LinearLayout) findViewById(R.id.button_automation_background);
        buttonAutomationForeground = (ImageButton) findViewById(R.id.button_automation_foreground);
        buttonAutomationBackgroundOpen = (LinearLayout) findViewById(R.id.button_automation_background_open);
        buttonAutomationForegroundOpen = (ImageButton) findViewById(R.id.button_automation_foreground_open);

        buttonAutomationBackground.setOnClickListener(toggleAutomationButton);
        buttonAutomationBackgroundOpen.setOnClickListener(toggleAutomationButton);
        
        //Loading animations just one time
        slideAutomationUpBack = AnimationUtils.loadAnimation(ButtonDeployableTopActivity.this, R.anim.button_background_slide_up);
        slideAutomationDownBack = AnimationUtils.loadAnimation(ButtonDeployableTopActivity.this, R.anim.button_background_slide_down);
        slideAutomationUpFore = AnimationUtils.loadAnimation(ButtonDeployableTopActivity.this, R.anim.button_background_slide_up);
        slideAutomationDownFore = AnimationUtils.loadAnimation(ButtonDeployableTopActivity.this, R.anim.button_background_slide_down);
        
        slideAutomationUpBack.setAnimationListener(slideAutomationUpBackground);
        slideAutomationDownBack.setAnimationListener(slideAutomationDownBackground);
        slideAutomationUpFore.setAnimationListener(slideAutomationUpForeground);
        slideAutomationDownFore.setAnimationListener(slideAutomationDownForeground);
	}
	
	OnClickListener toggleButton = new OnClickListener() {
		
		public void onClick(View v) {
			isPressed = !isPressed;
			
	        
	        if (isPressed) {
	        	v.startAnimation(slideUpBack);
				buttonForeground.startAnimation(slideDownFore);
	        } else {
	        	v.startAnimation(slideDownBack);
				buttonForeground.startAnimation(slideUpFore);
	        }
	        
		}
	};
	
	public int getNewYPosition(Animation animation) {
		Transformation trans = new Transformation();
		int endTime = (int) (animation.getStartTime() + animation.getDuration());
		animation.getTransformation(endTime, trans);
		
		Matrix matrixTrans = trans.getMatrix();
		
		float[] matrixVals = new float[9];
		matrixTrans.getValues(matrixVals);
//        float xTraveled = matrixVals[2];
        float yTraveled = matrixVals[5];
        
		Log.d("Translation", "yTraveled: " + yTraveled);
		
		return (int) yTraveled;
	}
	
	AnimationListener slideUpBackground = new AnimationListener() {
		
		public void onAnimationStart(Animation animation) {}
		
		public void onAnimationRepeat(Animation animation) {}
		
		public void onAnimationEnd(Animation animation) {
			RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) buttonBackground.getLayoutParams();
			originalPosition = lp.topMargin;
			int newYPos = getNewYPosition(animation);
			int Ypos = lp.topMargin + newYPos;
			Log.d("Translation Background UP", "newYpos: " + newYPos);
	        Log.d("Translation Background UP", "lp.topMargin: " + lp.topMargin);
	        Log.d("Translation Background UP", "Ypos: " + Ypos);
	        lp.setMargins(0, Ypos, 0, 0);
	        
	        buttonBackground.setLayoutParams(lp);
		}
	};
	
	AnimationListener slideDownBackground = new AnimationListener() {
		public void onAnimationStart(Animation animation) {
		}
		
		public void onAnimationRepeat(Animation animation) {}
		
		public void onAnimationEnd(Animation animation) {
			RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) buttonBackground.getLayoutParams();
	        lp.setMargins(0, originalPosition, 0, 0);
	        buttonBackground.setLayoutParams(lp);
		}
	};
	
	AnimationListener slideDownForeground = new AnimationListener() {
		
		public void onAnimationStart(Animation animation) {}
		
		public void onAnimationRepeat(Animation animation) {}
		
		public void onAnimationEnd(Animation animation) {
			RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) buttonForeground.getLayoutParams();
			originalPositionForeground = lp.topMargin;
			int newYPos = getNewYPosition(animation);
			int Ypos = lp.topMargin + newYPos;
			Log.d("Translation Foreground DOWN", "newYpos: " + newYPos);
	        Log.d("Translation Foreground DOWN", "lp.topMargin: " + lp.topMargin);
	        Log.d("Translation Foreground DOWN", "Ypos: " + Ypos);
	        lp.setMargins(0, Ypos, 0, 0);
	        
	        buttonForeground.setLayoutParams(lp);
		}
	};
	
	AnimationListener slideUpForeground = new AnimationListener() {
		public void onAnimationStart(Animation animation) {
		}
		
		public void onAnimationRepeat(Animation animation) {}
		
		public void onAnimationEnd(Animation animation) {
			RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) buttonForeground.getLayoutParams();
			lp.setMargins(0, originalPositionForeground, 0, 0);
			buttonForeground.setLayoutParams(lp);
		}
	};
	
	
	OnClickListener toggleAutomationButton = new OnClickListener() {
		
		public void onClick(View v) {
			isAutomationPressed = !isAutomationPressed;
			
	        if (isAutomationPressed) {
	        	buttonAutomationBackground.startAnimation(slideAutomationUpBack);
				buttonAutomationForeground.startAnimation(slideAutomationDownFore);
	        } else {
	        	buttonAutomationBackgroundOpen.startAnimation(slideAutomationDownBack);
				buttonAutomationForegroundOpen.startAnimation(slideAutomationUpFore);
	        }
		}
	};
	
	
	AnimationListener slideAutomationUpBackground = new AnimationListener() {
		
		public void onAnimationStart(Animation animation) {}
		
		public void onAnimationRepeat(Animation animation) {}
		
		public void onAnimationEnd(Animation animation) {
			if (isFirstTimeOpenBackground) {
				RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) buttonAutomationBackground.getLayoutParams();
				RelativeLayout.LayoutParams lpo = (RelativeLayout.LayoutParams) buttonAutomationBackgroundOpen.getLayoutParams();
				isFirstTimeOpenBackground = false;
				int newYPos = getNewYPosition(animation);
				int Ypos = lp.topMargin + newYPos;
		        lpo.setMargins(0, Ypos, 0, 0);
		        buttonAutomationBackgroundOpen.setLayoutParams(lpo);
			}
	        
	        buttonAutomationBackground.setVisibility(View.GONE);
	        buttonAutomationBackgroundOpen.setVisibility(View.VISIBLE);
		}
	};
	
	AnimationListener slideAutomationDownBackground = new AnimationListener() {
		public void onAnimationStart(Animation animation) {
		}
		
		public void onAnimationRepeat(Animation animation) {}
		
		public void onAnimationEnd(Animation animation) {
			buttonAutomationBackgroundOpen.setVisibility(View.GONE);
			buttonAutomationBackground.setVisibility(View.VISIBLE);
		}
	};
	
	AnimationListener slideAutomationDownForeground = new AnimationListener() {
		
		public void onAnimationStart(Animation animation) {}
		
		public void onAnimationRepeat(Animation animation) {}
		
		public void onAnimationEnd(Animation animation) {
			if (isFirstTimeOpenForeground) {
				RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) buttonAutomationForeground.getLayoutParams();
				RelativeLayout.LayoutParams lpo = (RelativeLayout.LayoutParams) buttonAutomationForegroundOpen.getLayoutParams();
				isFirstTimeOpenForeground = false;
				int newYPos = getNewYPosition(animation);
				int Ypos = lp.topMargin + newYPos;
		        lpo.setMargins(0, Ypos, 0, 0);
		        buttonAutomationForegroundOpen.setLayoutParams(lpo);
			}
	        
	        buttonAutomationForeground.setVisibility(View.GONE);
	        buttonAutomationForegroundOpen.setVisibility(View.VISIBLE);
		}
	};
	
	AnimationListener slideAutomationUpForeground = new AnimationListener() {
		public void onAnimationStart(Animation animation) {			
		}
		
		public void onAnimationRepeat(Animation animation) {}
		
		public void onAnimationEnd(Animation animation) {
			buttonAutomationForegroundOpen.setVisibility(View.GONE);
			buttonAutomationForeground.setVisibility(View.VISIBLE);
		}
	};

}

